package com.cskaoyan.service;

import com.cskaoyan.bean.Topic;
import com.cskaoyan.bean.TopicExample;
import com.cskaoyan.mapper.TopicMapper;
import com.cskaoyan.vo.TopicVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    TopicMapper topicMapper;
    @Override
    public TopicVo topicList(Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page,limit);

        //构造条件
        TopicExample topicExample = new TopicExample();
        topicExample.setOrderByClause(sort +" "+ order);
        List<Topic> topics = topicMapper.selectByExample(topicExample);

        PageInfo<Topic> topicPageInfo = new PageInfo<>();
        long total = topicPageInfo.getTotal();

        TopicVo topicVo = new TopicVo();
        topicVo.setItems(topics);
        topicVo.setTotal(total);
        return topicVo;
    }

    @Override
    public void topicDelete(Topic topic) {
        topicMapper.deleteByPrimaryKey(topic.getId());
    }

    @Override
    public Topic topicUpdate(Topic topic) {
        int i = topicMapper.updateByPrimaryKey(topic);
        Integer id = topic.getId();
        Topic data = topicMapper.selectByPrimaryKey(id);
        return data;
    }

    @Override
    public Topic topicCreate(Topic topic) {
        Date date = new Date();
        topic.setAddTime(date);
        topic.setUpdateTime(date);
        topicMapper.insertSelective(topic);
        Integer id = topic.getId();
        Topic data = topicMapper.selectByPrimaryKey(id);
        return data;
    }
}
